package com.somnus.androidkotlindemo.activity

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.LinearSnapHelper
import android.util.Log
import android.view.WindowManager
import com.somnus.androidkotlindemo.R
import com.somnus.androidkotlindemo.adapter.RecyclerViewAdapter
import com.somnus.androidkotlindemo.bean.UserBean
import com.somnus.androidkotlindemo.kt.toast
import kotlinx.android.synthetic.main.activity_recycler_view.*

/**
 *  原生的RecyclerView Adapter
 */
class RecyclerViewActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //防止截屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        setContentView(R.layout.activity_recycler_view)


        val adapter = RecyclerViewAdapter(initData(), this)
        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = adapter
        val snap = LinearSnapHelper()
        //每次只能滑动一页
//        val snap = PagerSnapHelper()
        snap.attachToRecyclerView(recycler)

        adapter.setOnClickItemListener { pos ->
            toast("$pos")
            Log.d("tag", "$pos")
        }


    }

    private fun initData(): List<UserBean> {
        var list = ArrayList<UserBean>()
        for (i in 0..50) {
            var user = UserBean()
            user.age = 18
            user.name = "测试" + i
            list.add(i, user)
        }
        return list
    }


}
